package com.hxd.otc.module.home.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;

import com.hxd.otc.R;
import com.hxd.otc.base.BaseRecyclerActivity;
import com.hxd.otc.common.enums.MessageTypeEnum;
import com.hxd.otc.module.convenience.activity.ConvenienceOrderDetailActivity;
import com.hxd.otc.module.home.adapter.MessageAdapter;
import com.hxd.otc.module.home.contract.MessageContract;
import com.hxd.otc.module.home.presenter.MessagePresenter;
import com.hxd.otc.module.my.activity.ExchangeDetailActivity;
import com.hxd.otc.module.my.activity.MessageDetailActivity;
import com.hxd.otc.module.my.bean.MessageBean;
import com.hxd.otc.module.order.activity.BuyOrderDetailActivity;
import com.hxd.otc.module.order.activity.OrderFinishedActivity;
import com.hxd.otc.module.order.activity.SaleOrderDetailActivity;
import com.hxd.otc.module.order.bean.AllOrderBean;
import com.hxd.otc.module.order.fragment.AllOrderFragment;

import static com.hxd.otc.module.order.fragment.AllOrderFragment.REQUEST_CODE_UPDATE_INFO;

/**
 * Created by lzy on 2019/1/17 0017.
 * description ${消息}
 */
public class MessageActivity extends BaseRecyclerActivity<MessagePresenter, MessageAdapter, MessageBean> implements MessageContract.IView {
    @Override
    protected MessageAdapter createAdapter() {
        return new MessageAdapter(this);
    }

    @Override
    protected MessagePresenter createPresenter() {
        return new MessagePresenter(this);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_message;
    }

    @Override
    public void onItemClick(int position) {
        super.onItemClick(position);
        MessageBean item = mAdapter.getItem(position);
        switch (MessageTypeEnum.getInstance(item.getType())) {
            case OTC_EXCHANGE://OTC交易
                onSkipDealOrderDetail(item);
                break;
            case CONVENIENCE://便民服务
                onSkipConvenienceOrderDetail(item);
                break;
            case COIN_EXCHANGE://币币兑换
                onSkipCoinExchangeDetail(item);
                break;
            case CIRCULATE_OR_EXCHANGE://通兑
            case NON_INDUCTIVE_PAY://无感支付
            case OTHER://其他
                onSkipMessageDetail(item);
                break;
        }
    }


    /**
     * 币币兑换详情
     *
     * @param item
     */
    private void onSkipCoinExchangeDetail(MessageBean item) {
        String orderId = item.getExtra();
        if (TextUtils.isEmpty(orderId)) return;

        Bundle bundle = new Bundle();
        bundle.putString(ExchangeDetailActivity.EXTRA_EXCHANGE_ID, orderId);
        $startActivity(ExchangeDetailActivity.class, bundle);
    }

    /**
     * 便民服务订单详情
     *
     * @param item
     */
    private void onSkipConvenienceOrderDetail(MessageBean item) {
        String orderId = item.getExtra();
        if (TextUtils.isEmpty(orderId)) return;
        Bundle bundle = new Bundle();
        bundle.putString(ConvenienceOrderDetailActivity.EXTRA_ID, orderId);
        $startActivity(ConvenienceOrderDetailActivity.class, bundle);
    }

    /**
     * 交易订单详情
     *
     * @param item
     */
    private void onSkipDealOrderDetail(MessageBean item) {
        String orderId = item.getExtra();
        if (TextUtils.isEmpty(orderId)) {
            onSkipMessageDetail(item);
        } else {
            mPresenter.requestDealOrderDetail(orderId);
        }
    }


    @Override
    public void requestDealOrderDetailSuccess(AllOrderBean.DataBean bean) {
        Bundle bundle = new Bundle();
        bundle.putSerializable(AllOrderFragment.ORDERINFO, bean);
        bundle.putBoolean(OrderFinishedActivity.ISNORMALORDER, true);
        Intent intent = new Intent();
        intent.putExtras(bundle);
        if (bean.status == SaleOrderDetailActivity.SALE_FINISHED) {
            $startActivity(OrderFinishedActivity.class, bundle);
        } else if (!bean.exType.equals("0")) {//0.卖出,1.买入
            intent.setClass(this, SaleOrderDetailActivity.class);
            startActivityForResult(intent, REQUEST_CODE_UPDATE_INFO);
        } else {
            intent.setClass(this, BuyOrderDetailActivity.class);
            startActivityForResult(intent, REQUEST_CODE_UPDATE_INFO);
        }
    }

    /**
     * 消息详情
     *
     * @param item
     */
    private void onSkipMessageDetail(MessageBean item) {
        Bundle bundle = new Bundle();
        bundle.putSerializable(MessageDetailActivity.EXTRA_MESSAGE_DETAIL, item);
        $startActivity(MessageDetailActivity.class, bundle);
    }
}
